﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using System.Data;
using System.Data.SqlClient;

namespace BLL
{
    public class SINHHIEUBLL
    {
        DataService DB = new DataService();

        public void insert(string nhiptho, string nhietdo, string huyetap, string mach, int mabenhan, DateTime ngaytao, string file_patch)
        {
            string sql = "insert into sinhhieu values (@nhiptho,@nhietdo,@huyetap,@mach,@mabenhan,@ngaytao,@file_patch)";

            SqlParameter paraNhiptho = new SqlParameter("nhiptho", nhiptho);
            SqlParameter paraNhietdo = new SqlParameter("nhietdo", nhietdo);
            SqlParameter paraHuyetap = new SqlParameter("huyetap", huyetap);
            SqlParameter paraMach = new SqlParameter("mach", mach);
            SqlParameter paraMaba = new SqlParameter("mabenhan", mabenhan);
            SqlParameter paraNgay = new SqlParameter("ngaytao", ngaytao);
            SqlParameter paraFile_patch = new SqlParameter("file_patch", file_patch);

            DB.ExecuteNonQuery(sql, paraNhiptho, paraNhietdo, paraHuyetap, paraMach, paraMaba, paraNgay, paraFile_patch);
        }

        public void update(int masinhhieu, string nhiptho, string nhietdo, string huyetap, string mach, int mabenhan, DateTime ngaytao, string file_patch)
        {
            string sql = "update sinhhieu set nhiptho = N'" + nhiptho + "', nhietdo = N'" + nhietdo + "', huyetap = N'" + huyetap + "' , mach = N'" + mach + "', mabenhan = " + mabenhan.ToString() + ", ngaytao = '" + ngaytao.ToString() + "', file_patch = '" + file_patch + "' where matt = " + masinhhieu.ToString();
            DB.ExecuteNonQuery(sql);
        }

        public void delete(int masinhhieu)
        {
            string sql = "delete from sinhhieu where masinhhieu = " + masinhhieu.ToString();
            DB.ExecuteNonQuery(sql);
        }

        public SINHHIEU getSinhhieuWithIdBaiviet(int idMabaiviet){
            string sql = "select masinhhieu,nhiptho,nhietdo,huyetap,mach,sh.mabenhan,ngaytao,sh.file_patch "
                        + " from sinhhieu sh,hosobenhan hsba,baiviet bv where sh.mabenhan = hsba.mabenhan "
                        + " and hsba.mabaiviet = bv.mabaiviet and bv.mabaiviet = " + idMabaiviet.ToString();

            DataTable table = new DataTable();
            table = DB.ExecuteReader(sql);
            DataRow r = table.Rows[0];

            SINHHIEU sh = new SINHHIEU();
            sh.masinhhieu = Int32.Parse( r[0].ToString());
            sh.nhiptho = r[1].ToString();
            sh.nhietdo = r[2].ToString();
            sh.huyetap = r[3].ToString();
            sh.mach = r[4].ToString();
            sh.mabenhan = Int32.Parse( r[5].ToString());
            sh.ngaytao = Convert.ToDateTime(r[6].ToString());
            try { sh.file_patch = r[7].ToString(); }
            catch { sh.file_patch = ""; }

            return sh;
        }

        public void updateSinhhieuWithID(int masinhhieu,string nhiptho, string nhietdo, string huyetap,string mach, 
            int mabenhan, DateTime ngaytao, string file_patch) {

                string sql = "update sinhhieu set nhiptho = N'" + nhiptho + "', nhietdo = N'" + nhietdo + "', huyetap = N'" + huyetap + "', "
            + " mach = N'" + mach +  "', mabenhan = " + mabenhan.ToString() + ", ngaytao = '" + ngaytao.ToString() + "'," +
            " file_patch = '" + file_patch + "' where masinhhieu = " + masinhhieu.ToString();

                DB.ExecuteNonQuery(sql);
        }
    }
}
